package com.omesoft.hksnore.util.dao;


import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.omesoft.hksnore.util.dbhelper.DBHelper4Sleep;
import com.omesoft.hksnore.util.dbhelper.SetData;
import com.omesoft.hksnore.util.entity.Medix_Pub_Sync_SleepDetail;
import com.omesoft.hksnore.util.snore.SnoreEntity;

import java.util.ArrayList;
import java.util.List;


/**
 * 鼾声（snore表）
 *
 * @author trs
 *
 */

public class SnoreIfcImpl implements SnoreIfc {

	private DBHelper4Sleep dbHelper;

	public SnoreIfcImpl(Context context) {
		// TODO Auto-generated constructor stub
		dbHelper = DBHelper4Sleep.getInstance(context, SetData.DATATBASE_NAME_MendaleSleep);
	}


	public void insert(SnoreEntity entity) {
		// TODO Auto-generated method stub
		dbHelper.insertToSnore(entity.getMemberID(), entity);
	}


	public void insert(List<SnoreEntity> list) {
		// TODO Auto-generated method stub
		dbHelper.insertOrUpdate2Snore(list);
	}

	@Override
	public void insertSnoreTime(int memberID,SnoreEntity list) {
		dbHelper.insertToSnore(memberID,list);

	}

	@Override
	public List<SnoreEntity> findAllBySleepID(String SleepID) {
		// TODO Auto-generated method stub
		List<SnoreEntity> data = new ArrayList<SnoreEntity>();
		Cursor cursor = dbHelper.findSnoreBySleepID(SleepID);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				SnoreEntity entity = new SnoreEntity();
				entity.setSleepID(SleepID);
				entity.setSnoreID(cursor.getString(cursor.getColumnIndexOrThrow("SnoreID")));
				entity.setMemberID(cursor.getInt(cursor.getColumnIndexOrThrow("MemberID")));
				entity.setDecibels(cursor.getString(cursor.getColumnIndexOrThrow("Decibels")));
				entity.setSampleCount(cursor.getInt(cursor.getColumnIndexOrThrow("SampleCount")));
				entity.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
				entity.setRecordDate(cursor.getString(cursor.getColumnIndexOrThrow("RecordDate")));
				entity.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
				entity.setUpdateDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
				entity.setIsDeleted(cursor.getInt(cursor.getColumnIndexOrThrow("isDeleted")));
				entity.setSnoreStartTime(cursor.getString(cursor.getColumnIndexOrThrow("SnoreStartTime")));
				entity.setSnoreEndTime(cursor.getString(cursor.getColumnIndexOrThrow("SnoreEndTime")));
				data.add(entity);
				Log.i("hehe", "findAllBySleepID: "+data.toString());
			}
			cursor.close();
			cursor = null;
		}
		return data;
	}

	@Override
	public List<SnoreEntity> findSnoreTimeBySleepID(String SleepID) {
		return null;
	}

	@Override
	public List<SnoreEntity> findAllByMemberID(int memberID, String timeStamp) {
		// TODO Auto-generated method stub
		List<SnoreEntity> data = new ArrayList<SnoreEntity>();
		Cursor cursor = dbHelper.findSnoreByMemberID(memberID, timeStamp);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				SnoreEntity entity = new SnoreEntity();
				entity.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
				entity.setSnoreID(cursor.getString(cursor.getColumnIndexOrThrow("SnoreID")));
				entity.setMemberID(cursor.getInt(cursor.getColumnIndexOrThrow("MemberID")));
				entity.setDecibels(cursor.getString(cursor.getColumnIndexOrThrow("Decibels")));
				entity.setSampleCount(cursor.getInt(cursor.getColumnIndexOrThrow("SampleCount")));
				entity.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
				entity.setRecordDate(cursor.getString(cursor.getColumnIndexOrThrow("RecordDate")));
				entity.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
				entity.setUpdateDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
				entity.setIsDeleted(cursor.getInt(cursor.getColumnIndexOrThrow("isDeleted")));
				data.add(entity);
			}
			cursor.close();
			cursor = null;
		}
		return data;
	}

	@Override
	public List<Medix_Pub_Sync_SleepDetail> findAllBySleepID2(String SleepID) {
		// TODO Auto-generated method stub
		List<Medix_Pub_Sync_SleepDetail> data = new ArrayList<Medix_Pub_Sync_SleepDetail>();
		Cursor cursor = dbHelper.findSnoreBySleepID(SleepID);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				Medix_Pub_Sync_SleepDetail entity = new Medix_Pub_Sync_SleepDetail();
				entity.setSleepID(cursor.getString(cursor.getColumnIndexOrThrow("SleepID")));
				entity.setMemberID(cursor.getInt(cursor.getColumnIndexOrThrow("MemberID")));
				entity.setSleepQuality(Float.parseFloat(cursor.getString(cursor.getColumnIndexOrThrow("Decibels"))));
				entity.setSampleCount(cursor.getInt(cursor.getColumnIndexOrThrow("SampleCount")));
				entity.setSnoreCount(cursor.getInt(cursor.getColumnIndexOrThrow("SnoreCount")));
				entity.setSnoreStopCount(cursor.getInt(cursor.getColumnIndexOrThrow("StopSnoreCount")));
				entity.setCreatedDate(cursor.getString(cursor.getColumnIndexOrThrow("CreatedDate")));
				entity.setRecordDate(cursor.getString(cursor.getColumnIndexOrThrow("RecordDate")));
				entity.setUpdatedDate(cursor.getString(cursor.getColumnIndexOrThrow("UpdatedDate")));
				data.add(entity);
			}
			cursor.close();
			cursor = null;
		}
		return data;
	}

}
